﻿
//***********************************************************************
// * Module:   TbBstDriverHandler
// * 作者:  曹荣芝
// * Modified: 2013-05-09
// * Purpose: Definition of the Class TbBstDriverHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstDriverHandler 的摘要说明
    /// </summary>
    public class TbBstDriverHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            // HttpContext context = HttpContext.Current;
            //context.Response.ContentType = "text/plain";
            //ResultModel mResultModel = new ResultModel(); //初始化返回类型
            //try
            //{

            //TbBstDriver mTbBstDriver = new TbBstDriver();
            //mTbBstDriver.DriverPkid = Guid.NewGuid().ToString(); //主键
            //     mTbBstDriver.UserId = context.Request.Form["texUserId"]; //用户编号 同用户表关联
            //     mTbBstDriver.DriverName = context.Request.Form["texDriverName"]; //驾驶员姓名
            //     mTbBstDriver.DriverCode = context.Request.Form["texDriverCode"]; //驾驶员驾证号码
            //     mTbBstDriver.DriverAddress = context.Request.Form["texDriverAddress"]; //现居地地址
            //     mTbBstDriver.DriverScore = Convert.ToDecimal(context.Request.Form["texDriverScore"]); //积分
            //     mTbBstDriver.UpdateDate = context.Request.Form["texUpdateDate"]; //修改时间
            //     mTbBstDriver.DriverPhone = context.Request.Form["texDriverPhone"]; //驾驶员手机号
            //     mTbBstDriver.DriverIdcard = context.Request.Form["texDriverIdcard"]; //身份证号
            //     mTbBstDriver.Registereddate = context.Request.Form["texRegistereddate"]; //注册时间
            //     mTbBstDriver.Movesign = context.Request.Form["texMovesign"]; //是否同步  0需要   1不需要

            //mResultModel = TbBstDriverBLL.Insert(mTbBstDriver,context.Session["UserID"], true); //新建操作
            //context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            //}
            //catch (Exception ex)
            //{
            //    MSEx.DealErr(ex, "");
            //    mResultModel.ErrorCode = -9999;
            //    mResultModel.ISSucceed = false;
            //    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            //}
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstDriver mTbBstDriver = new TbBstDriver();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = TbBstDriverBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion
        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstDriver mTbBstDriver = new TbBstDriver();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstDriver = TbBstDriverBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstDriver != null)
                {
                    mTbBstDriver.UserId = context.Request.Form["texUserId"]; //用户编号 同用户表关联
                    mTbBstDriver.DriverName = context.Request.Form["texDriverName"]; //驾驶员姓名
                    mTbBstDriver.DriverCode = context.Request.Form["texDriverCode"]; //驾驶员驾证号码
                    mTbBstDriver.DriverAddress = context.Request.Form["texDriverAddress"]; //现居地地址
                    mTbBstDriver.DriverScore = context.Request.Form["texDriverScore"]; //积分
                    mTbBstDriver.UpdateDate = DateTime.Now; //修改时间
                    mTbBstDriver.DriverPhone = context.Request.Form["texDriverPhone"]; //驾驶员手机号
                    mTbBstDriver.DriverIdcard = context.Request.Form["texDriverIdcard"]; //身份证号
                    mTbBstDriver.Movesign = context.Request.Form["texMovesign"]; //是否同步  0需要   1不需要

                    mResultModel = TbBstDriverBLL.Update(mTbBstDriver, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                TbBstDriver mTbBstDriver = new TbBstDriver(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstDriver = TbBstDriverBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstDriver != null)
                    {
                        mTbBstDriver.DriverPhone = DES.Decode(mTbBstDriver.DriverPhone);
                        mTbBstDriver.DriverIdcard = DES.Decode(mTbBstDriver.DriverIdcard);
                        IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                        json = JsonConvert.SerializeObject(mTbBstDriver, dtConverter);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstDriver = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            string name = context.Request.Form["texName"];
            string idcard = DES.Encode(context.Request.Form["txtIDcard"]);
            string phone = DES.Encode(context.Request.Form["TexPhone"]);
            string address = context.Request.Form["TexAddress"];
            string begintime = context.Request.Form["texTimeStart"];
            string endtime = context.Request.Form["texTimeEnd"];
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();
            SqlConditionList sqllist = new SqlConditionList();
            if (!string.IsNullOrEmpty(name))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstDriverConst.Columns.DriverName, name), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(idcard))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstDriverConst.Columns.DriverIdcard, idcard), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(phone))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstDriverConst.Columns.DriverPhone, phone), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(address))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstDriverConst.Columns.DriverAddress, address), SqlLogicOper.AND_LOGIC);

            }
            if (!string.IsNullOrEmpty(begintime))
            {
                DateTime dtstart = new DateTime();
                if (DateTime.TryParse(begintime, out dtstart))
                {
                    sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_LESS_THAN, TbBstDriverConst.Columns.Registereddate, dtstart), SqlLogicOper.AND_LOGIC);//Convert.ToDateTime(dtstart.ToString("yyyy-MM-dd hh:00:01")))
                }
            }
            if (!string.IsNullOrEmpty(endtime))
            {
                DateTime dtend = new DateTime();
                if (DateTime.TryParse(endtime, out dtend))
                {
                    sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_MORE_THAN, TbBstDriverConst.Columns.Registereddate, dtend), SqlLogicOper.AND_LOGIC);//Convert.ToDateTime(dtend.ToString("yyyy-MM-dd hh:59:59")))
                }
            }
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstDriverConst.Columns.Isdel, "0"), SqlLogicOper.AND_LOGIC);   //0未删除  1删除
            dtTbBstDriver = Q.GetRecordByPage("DRIVER_PKID,DRIVER_NAME,DRIVER_PHONE,DRIVER_IDCARD,DRIVER_ADDRESS,REGISTEREDDATE", sqllist.ToSql(), "REGISTEREDDATE", intPageSize, intPageIndex, TbBstDriverConst.TableName, TbBstDriverConst.FieldKey, "").Tables[0];

            if (dtTbBstDriver != null && dtTbBstDriver.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstDriver.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstDriver.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtTbBstDriver;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            for (int i = 0; i < dtTbBstDriver.Rows.Count; i++)
            {
                for (int j = 2; j < 4; j++)
                {
                    string decode = dtTbBstDriver.Rows[i][j].ToString();
                    dtTbBstDriver.Rows[i][j] = DES.Decode(decode);
                }
            }
            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string json = JsonConvert.SerializeObject(tm, dtConverter);
            context.Response.Write(json);
        }
        #endregion

    }
}


